<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/2/7
 * Time: 18:52
 */

namespace app\index\controller;


class Sjx extends Common
{
    public function index(){
        $title = $this->request->get('title');
        $this->assign('title',$title);
        $lx = $this->request->get('lx');
        $this->assign('lx',$lx);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        $de_name = $this->request->get('de_name');
        $this->assign('de_name',$de_name);
        $fqr_name = $this->request->get('fqr_name');
        $this->assign('fqr_name',$fqr_name);
        $is_read = $this->request->param('is_read');
        $this->assign('is_read',$is_read);
        $is_star = $this->request->param('is_star');
        $this->assign('is_star',$is_star);
        $where = [];
        $where['sjr_id'] = session('employee_id');
        if($is_read!==null){
           $where['is_read'] = $is_read;
        }
        if($is_star!==null){
           $where['is_star'] = $is_star;
        }
        if($title){
            $where['title'] = ['like',"%{$title}%"];
        }
        if($lx){
            $where['lx'] = $lx;
        }
        if($start&&!$end){
            $where['cssj'] = ['>',$start];
        }elseif($end&&!$start){
            $where['cssj'] = ['<',$end];
        }elseif($start&&$end){
            $where['cssj'] = [['>',$start],['<',$end],'and'];
        }
        if($sn){
            $where['sn'] = ['like',"%{$sn}%"];
        }
        if($de_name){
            $where['de_name'] = ['like',"%{$de_name}%"];
        }
        if($fqr_name){
            $where['fqr_name'] = ['like',"%{$fqr_name}%"];
        }
        $count = db('cg_mail')->where($where)->count();
        $data = db('cg_mail')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['lxmc'] = db('cg_mail_lx')->where('id',$v['lx'])->value('title');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        $lxs = db('cg_mail_lx')->select();
        $this->assign('lxs',$lxs);
        return $this->fetch();
    }

    //标记为已读或未读
    public function mark_read(){
        $type = $this->request->param('type');
        $data = $this->request->post('data/a');
        $where = [];
        $where['id'] = ['in',$data];
        $bool = db('cg_mail')->where($where)->update(['is_read'=>$type]);
        if(false !== $bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    //标星
    public function mark_star(){
        $type = $this->request->param('type');
        $data = $this->request->post('data/a');
        $where = [];
        $where['id'] = ['in',$data];
        $bool = db('cg_mail')->where($where)->update(['is_star'=>$type]);
        if(false !== $bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }
    //详情页
    public function view(){
        $id = $this->request->param('id');
        $data = db('cg_mail')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
}